package com.jie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.constants.RedisCountConst;
import com.jie.dto.CombinationTopicDTO;
import com.jie.dto.ThematicCategoryDTO;
import com.jie.entity.ThematicCategory;
import com.jie.exception.BizException;
import com.jie.mapper.ThematicCategoryMapper;
import com.jie.service.IThematicCategoryService;
import com.jie.utils.BeanCopyUtils;
import com.jie.vo.ConditionVO;
import com.jie.vo.PageResult;
import com.jie.vo.ThematicCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 专题大类 服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-04-21
 */
@Service
public class ThematicCategoryServiceImpl extends ServiceImpl<ThematicCategoryMapper, ThematicCategory> implements IThematicCategoryService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 分页查询
     * @param conditionVO
     * @return
     */
    @Override
    public PageResult<ThematicCategoryDTO> getThematicCategoryPageInfo(ConditionVO conditionVO) {
        conditionVO.setCurrent(conditionVO.getCurrent() > 0 ? conditionVO.getCurrent() : 1);
        conditionVO.setSize(conditionVO.getSize() > 0 ? conditionVO.getSize() : 10);
        Page<ThematicCategory> page = new Page<>(conditionVO.getCurrent(), conditionVO.getSize());
        // 查询专题列表
        Page<ThematicCategory> thematicCategoryPage = this.page(page, new LambdaQueryWrapper<ThematicCategory>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), ThematicCategory::getTipicClassName, conditionVO.getKeywords())
                .or()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), ThematicCategory::getTipicClassInfo, conditionVO.getKeywords())
                .orderByDesc(ThematicCategory::getTipicPersonCount));
        List<ThematicCategoryDTO> operationLogDTOList = BeanCopyUtils.copyList(thematicCategoryPage.getRecords(), ThematicCategoryDTO.class);
        return new PageResult<>(operationLogDTOList, operationLogDTOList.size());
    }

    /**
     * 添加或更新专题信息
     * @param thematicCategoryVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveThematicCategory(ThematicCategoryVO thematicCategoryVO) {
        if (Objects.isNull(thematicCategoryVO)){
            throw new BizException("传入对象为空，系统错误");
        }
        ThematicCategory thematicCategory = BeanCopyUtils.copyObject(thematicCategoryVO, ThematicCategory.class);

        return this.saveOrUpdate(thematicCategory);
    }

    /**
     *  删除专题信息
     * @param thematicCategoryId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteThematicCategory(Integer thematicCategoryId) {
        //后续这里应该要对专题下的训练计划进行判断，如果训练计划存在，则不允许删除
        return this.removeById(thematicCategoryId);
    }

    @Override
    public ThematicCategory getThematicCategoryPageInfoById(Integer thematicCategoryId) {
        return this.baseMapper.selectById(thematicCategoryId);
    }

    @Override
    public CombinationTopicDTO getThematicCategoryPageInfoByName(List<String> thematicCategoryName) {
        return null;
    }
}
